﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniMana.Utils.Controls;
using System.Data.Linq;

namespace UniMana.Data
{
    public class Training_SubjectClass_ScoreTypeInfo
    {
        private bool m_bCheck = false;
            #region /*Không thay đổi*/
            public bds m_bds = new bds();
            public void Save()
            {
                m_bds.EndEdit();
                m_data.m_DCSave.SubmitChanges();
            }
            public void Delete()
            {
                m_bds.DeleteCurrent();
            }
            #endregion /*Không thay đổi*/
            private DBMain m_data = new DBMain();
            #region [Properties - Thuộc tính]
        
            public Training_SubjectClass_ScoreType Properties
            {
                get { return m_bds.Current as Training_SubjectClass_ScoreType; }
            }
            #endregion
            public Training_SubjectClass_ScoreTypeInfo( int subjectClassKey = 0, int scoreTypeKey = 0)
            {
                var IQuery = m_data.m_DCSave.Training_SubjectClass_ScoreTypes.Where(tb => tb.Training_Subject_ClassKey == subjectClassKey 
                                            && tb.Training_ScoreTypeKey == scoreTypeKey);
                m_bds.DataSource = IQuery;
                var Row = IQuery.FirstOrDefault();
                if (Row == null)
                {
                    Training_SubjectClass_ScoreType _new  = new Training_SubjectClass_ScoreType();
                    _new.Training_Subject_ClassKey = subjectClassKey;
                    _new.Training_ScoreTypeKey = scoreTypeKey;
                    Row = _new;
                    m_bds.Add(Row);
                    m_bCheck = true;
                }
            }
            /// <summary>
            /// Lấy thông tin loại điểm của một lớp học phần
            /// </summary>
            /// <param name="subjectClassKey">Mã lớp học phần</param>
            public IQueryable GetInfo(Table<Training_SubjectClass_ScoreType> t1, Table<Training_ScoreType> t2, int subjectClassKey)
            {
                var ss = from s in t1
                         join ts in t2 on s.Training_ScoreTypeKey equals ts.Training_ScoreTypeKey
                         where s.Training_Subject_ClassKey == subjectClassKey
                         select new
                         {
                             s.Training_Subject_ClassKey,
                             s.Training_ScoreTypeKey,
                             ts.Name,
                             s.DefaultPercent,
                             s.Note
                         };
                return ss;
            }
            public bool CheckClassScoreType(Table<Training_SubjectClass_ScoreType> t1, int subjectClassKey, int scoreTypeKey)
            {
                var s = from sc in t1
                        where sc.Training_Subject_ClassKey == subjectClassKey
                        where sc.Training_ScoreTypeKey == scoreTypeKey
                        select sc;
                if (s.Count() >0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            public bool CheckScoreTypeCount(Table<Training_SubjectClass_ScoreType> t1, int subjectClassKey)
            {
                var s = from sc in t1
                        where sc.Training_Subject_ClassKey == subjectClassKey
                        select sc;
                if (s.Count() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            public bool Check()
            {
                return m_bCheck;
            }
            
    }
}
